﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace FalionForm
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ScrewRobot"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ScrewRobot;assembly=ScrewRobot"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:ScreenTextBox/>
    ///
    /// </summary>
    public class ScreenTextBox : TextBox
    {
        public enum PLCDataType{SignedDecimal, UnsignedDecimal, Hex, Bin, Float, Double, Text};
        static ScreenTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenTextBox), new FrameworkPropertyMetadata(typeof(ScreenTextBox)));
        }
        public ScreenTextBox()
        {
            this.PreviewMouseLeftButtonUp += ContentClicked;
        }
        #region 属性定义

        public static readonly DependencyProperty IsPasswordProperty =
            DependencyProperty.Register(
                "IsPassword", typeof(Boolean), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata(false));
        public Boolean IsPassword
        {
            get { return (Boolean)GetValue(IsPasswordProperty); }
            set { SetValue(IsPasswordProperty, value); }
        }
        public static readonly DependencyProperty AddressProperty =
            DependencyProperty.Register(
                "Address", typeof(string), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata(""));
        public string Address
        {
            get { return (string)GetValue(AddressProperty); }
            set { SetValue(AddressProperty, value); }
        }
        public static readonly DependencyProperty DataTypeProperty =
            DependencyProperty.Register(
                "DataType", typeof(PLCDataType), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata(PLCDataType.SignedDecimal));
        public PLCDataType DataType
        {
            get { return (PLCDataType)GetValue(DataTypeProperty); }
            set { SetValue(DataTypeProperty, value); }
        }
        public static readonly DependencyProperty DecimalDigitsProperty =
            DependencyProperty.Register(
                "DecimalDigits", typeof(int), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata(0));
        public int DecimalDigits
        {
            get { return (int)GetValue(DecimalDigitsProperty); }
            set { SetValue(DecimalDigitsProperty, value); }
        }
        public static readonly DependencyProperty ChannelCountProperty =
            DependencyProperty.Register(
                "ChannelCount", typeof(uint), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata((uint)0));
        public uint ChannelCount
        {
            get { return (uint)GetValue(ChannelCountProperty); }
            set { SetValue(ChannelCountProperty, value); }
        }
        public static readonly DependencyProperty MinlimitProperty =
            DependencyProperty.Register(
                "Minlimit", typeof(double), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata((double)0));
        public double Minlimit
        {
            get { return (double)GetValue(MinlimitProperty); }
            set { SetValue(MinlimitProperty, value); }
        }
        public static readonly DependencyProperty MaxlimitProperty =
            DependencyProperty.Register(
                "Maxlimit", typeof(double), typeof(ScreenTextBox),
                new FrameworkPropertyMetadata((double)0));
        public double Maxlimit
        {
            get { return (double)GetValue(MaxlimitProperty); }
            set { SetValue(MaxlimitProperty, value); }
        }
        #endregion
        #region 事件定义
        public static readonly RoutedEvent TriggeredEvent =
            EventManager.RegisterRoutedEvent("Triggered", RoutingStrategy.Direct,
                  typeof(TriggeredEventHandler), typeof(ScreenTextBox));
        public delegate void TriggeredEventHandler(object sender, RoutedEventArgs e);
        public event RoutedEventHandler Triggered
        {
            add { AddHandler(TriggeredEvent, value); }
            remove { RemoveHandler(TriggeredEvent, value); }
        }
        #endregion
        private void ContentClicked(object sender, RoutedEventArgs e)
        {
            //TextBox box = sender as TextBox;
            //string s;
            //if (box != null && box.IsReadOnly == false)
            //{
            //    ScreenKeyLib.WndInput dlg = new WndInput(KeyboardType);
            //    dlg.IsPassword = IsPassword;
            //    switch (KeyboardType)
            //    {
            //        case WndInput.enuPadType.PadFull:
            //            dlg.SetRange(Minlimit, Maxlimit, (int)this.ChannelCount);
            //            break;
            //        default:
            //            dlg.SetRange(Minlimit, Maxlimit, (int)this.DecimalDigits);
            //            break;
            //    }
            //    s = dlg.ShowDialog("");
            //    if (s != box.Text)
            //    {
            //        box.Text = s;
            //        RaiseEvent(new RoutedEventArgs(TriggeredEvent, this));
            //    }
            //}
            this.SelectAll();
        }
    }
}
